package com.qk.management.service.impl;

import com.qk.common.PageResult;
import com.qk.entity.Role;
import com.qk.management.mapper.RoleMapper;
import com.qk.management.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.role;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public void add(Role role) {
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.add(role);
    }

    @Override
    public Role findById(Integer id) {
        return roleMapper.findById(id);
    }

    @Override
    public void update(Role role) {
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.update(role);
    }

    @Override
    public List<Role> rolelist() {
        List<Role> rolelist = roleMapper.rolelists();
        return rolelist;
    }

    @Override
    public void deleteById(Integer id) {
        roleMapper.deleteById(id);
    }
    @Override
    public PageResult<Role> page(String name, String label, Integer pageNum, Integer pageSize) {
        Integer totalCount = roleMapper.count(name, label);
        Integer offSet = (pageNum - 1) * pageSize;
        List<Role> rolelist = roleMapper.selectByPage(name, label, offSet, pageSize);
        return PageResult.<Role>builder().total(totalCount).rows(rolelist).build();
    }
}
